<template>
  <div>
    <!-- 面包屑导航 -->
    <tab-control :title="['首页','商品管理','商品列表']"></tab-control>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoods"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoods"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表 -->
      <el-table :data="goodsInfo" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="95px"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="70px"
        ></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="getGoodById(scope.row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesiz"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>

      <!-- 编辑对话框 -->
      <el-dialog title="编辑商品" :visible.sync="goodsVisible" width="50%">
        <el-form ref="editGoodFormRef" :rules="editGoodFormRules" :model="editGoodForm" label-width="100px">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="editGoodForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="goods_price">
            <el-input v-model="editGoodForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="goods_number">
            <el-input v-model="editGoodForm.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="重量" prop="goods_weight">
            <el-input v-model="editGoodForm.goods_weight"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="goodsVisible = false">取 消</el-button>
          <el-button type="primary" @click="editGoods">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import TabControl from '@/components/tabcontrol/TabControl.vue'
import { getGoods, removeById ,editGoods} from "@/api/utils";
export default {
  name: "goods",
  components:{
    TabControl
  },
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      goodsInfo: [],
      total: 0,
      goodsVisible:false,
      editGoodForm:{
        goods_id:'',
        goods_name:'',
        goods_price:0,
        goods_number:0,
        goods_weight:0
      },
      editGoodFormRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_cat: [
          { required: true, message: "请选择商品分类", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getGoods();
  },
  methods: {
    // 获取商品列表
    getGoods() {
      getGoods(this.queryInfo).then((res) => {
        // console.log(res);
        if (res.meta.status != 200)
          return this.$message.error("获取商品列表数据失败!");
        this.goodsInfo = res.data.goods;
        this.total = res.data.total;
      });
    },
    //每页显示条数发生改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoods();
    },
    //页码发生改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoods();
    },
    //删除商品
    removeById(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          removeById(id).then((res) => {
            console.log(res);
            if (res.meta.status != 200)
              return this.$message.error(res.meta.msg);
            this.$message.success(res.meta.msg);
            this.getGoods();
          });
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    // 跳转添加商品页
    goAddpage() {
      this.$router.push("/goods/add");
    },
    // 打开编辑对话框获取商品数据
    getGoodById(goodInfo){
      this.goodsVisible=true
      for(let k in goodInfo){
        this.editGoodForm[k]=goodInfo[k]
      }
      console.log(this.editGoodForm);
    },
    // 提交编辑商品
    editGoods(){
      this.$refs.editGoodFormRef.validate(valid=>{
        if(!valid) return this.$message.error('请填写必要的参数')
        editGoods(this.editGoodForm).then(res=>{
          console.log(res);
          if(res.meta.status!=201) return this.$message.error(res.meta.msg)
          this.$message.success(res.meta.msg)
          this.getGoods()
          this.goodsVisible=false
        })
      })
    }
  },
};
</script>

<style lang="less" scoped>
</style>